package com.Atao.service.impl;

import com.Atao.service.FileDelete;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FileDeleteImpl implements FileDelete {

    private final OSS ossClient;

    private String bucketName = "big-event-atao";

    public void deleteFile(String objectKey) {
        try {
            // 验证对象是否存在（可选）
            boolean exists = ossClient.doesObjectExist(bucketName, objectKey);
            if (!exists) {
                throw new RuntimeException("文件不存在");
            }

            // 删除文件
            ossClient.deleteObject(bucketName, objectKey);
        } catch (OSSException | ClientException e) {
            throw new RuntimeException("删除失败: " + e.getMessage());
        }
    }

    // 新增批量删除方法
    public void deleteFiles(List<String> objectKeys) {
        for (String objectKey : objectKeys) {
            try {
                if(objectKey != null|| objectKeys.isEmpty()) {
                    deleteFile(objectKey);
                }
            } catch (Exception e) {
                System.err.println("删除文件失败: " + objectKey + ", 原因: " + e.getMessage());
            }
        }
    }
}